package com.fenghuajueli.libbasecoreui.manager;

import com.fenghuajueli.lib_data.entity.db.MattersDbEntity;
import com.fenghuajueli.lib_data.entity.db.MattersDbEntityDao;
import com.fenghuajueli.lib_data.entity.db.MemoDbEntity;
import com.fenghuajueli.lib_data.entity.db.MemoDbEntityDao;
import com.fenghuajueli.libbasecoreui.BaseApplication;

import java.util.List;

public class MattersManager {

    private static class MattersManagerSingleton {
        private static final MattersManager INSTANCE = new MattersManager();

    }

    public static MattersManager getInstance() {
        return MattersManager.MattersManagerSingleton.INSTANCE;
    }

    private MattersManager() {
    }


    public void saveMattersDbEntity(MattersDbEntity mattersDbEntity) {
        List<MattersDbEntity> list = BaseApplication.getMattersDaoMaster().newSession().getMattersDbEntityDao()
                .queryBuilder().where(MattersDbEntityDao.Properties.Create_time.eq(mattersDbEntity.getCreate_time())).list();
        if (list != null && list.size() > 0) {
            BaseApplication.getMattersDaoMaster().newSession().getMattersDbEntityDao().updateInTx(mattersDbEntity);
        } else {
            BaseApplication.getMattersDaoMaster().newSession().getMattersDbEntityDao().insertOrReplace(mattersDbEntity);
        }
    }

    public List<MattersDbEntity> getMattersData(int type) {
        List<MattersDbEntity> list = BaseApplication.getMattersDaoMaster().newSession().getMattersDbEntityDao()
                .queryBuilder().where(MattersDbEntityDao.Properties.Type.eq(type)).list();
        return list;
    }

    public List<MattersDbEntity> deleteMattersDbEntity(MattersDbEntity mattersDbEntity) {
        List<MattersDbEntity> list = BaseApplication.getMattersDaoMaster().newSession().getMattersDbEntityDao()
                .queryBuilder().where(MattersDbEntityDao.Properties.Create_time.eq(mattersDbEntity.getCreate_time())).list();
        if (list != null && list.size() > 0) {
            BaseApplication.getMattersDaoMaster().newSession().getMattersDbEntityDao().deleteInTx(list);
        }
        return getMattersData(mattersDbEntity.getType());
    }

    public List<MattersDbEntity> updateMattersDbEntity(MattersDbEntity mattersDbEntity,int update) {
        if (update == 1) {
            mattersDbEntity.setIs_complete(!mattersDbEntity.getIs_complete());
            BaseApplication.getMattersDaoMaster().newSession().getMattersDbEntityDao().updateInTx(mattersDbEntity);
        } else if (update == 2) {
            List<MattersDbEntity> list = getMattersData(mattersDbEntity.getType());
            for (MattersDbEntity entity : list) {
                entity.setIs_top(false);
                if (entity.getCreate_time() == mattersDbEntity.getCreate_time()) {
                    entity.setIs_top(true);
                }
            }
            BaseApplication.getMattersDaoMaster().newSession().getMattersDbEntityDao().updateInTx(list);
        }

        return getMattersData(mattersDbEntity.getType());
        /*List<MattersDbEntity> list = BaseApplication.getMattersDaoMaster().newSession().getMattersDbEntityDao()
                .queryBuilder().where(MattersDbEntityDao.Properties.Create_time.eq(mattersDbEntity.getCreate_time())).list();
        if (list != null && list.size() > 0) {
        }*/
    }

}
